package com.struct{
	/**
	 * 创建者：薛恒杰
	 * 工程名称：Marbles
	 * 创建时间：Jul 30, 2012 12:02:02 PM
	 * 类说明：Object.as,
	 */
	import com.collection.ActionCollection;
	import com.event.SoldierEvent;
	import com.role.Base;
	import com.role.MoveBase;
	import com.role.Soldier;
	import com.utils.Arrow;
	import com.utils.Collide;
	import com.utils.Physical;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	public class Soldier_Layer extends Sprite{
		public var soldiers:Array;//士兵数组
		public var movement:Array;//运动数组
		private var timer:Timer;//只是一个Timer
		
		private var id:uint;//当前士兵下标
		private var current:Soldier;//当前士兵对象
		private var arrow:Arrow;//移动提示箭头
		public function Soldier_Layer()
		{
			super();
			timer = new Timer(0);
			timer.addEventListener(TimerEvent.TIMER, timerHandler);
			arrow = new Arrow();
			addChild(arrow);
			arrow.visible = false;
			soldiers = [];
			movement = [];
			id = 0;
		}
		/**
		 * 加入一个士兵到战场
		 */
		public function addSoldier(s:Soldier):void{
			if(soldiers.indexOf(s) == -1){
				soldiers.push(s);
				addChild(s);
			}
		}
		/**
		 * 下一个士兵
		 */
		public function nextSoldier():void{
			if(id >= soldiers.length){
				id = 0;
			}
			current = soldiers[id] as Soldier;
			current.selected(true);
			arrow.x = current.x;
			arrow.y = current.y;
			current.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			++id;
		}
		/**
		 * 移除一个士兵
		 */
		public function removeSoldier(s:Soldier):void{
			var index:int = soldiers.indexOf(s);
			if(index != -1){
				soldiers.splice(index, 1);
				removeChild(s);
			}
		}
		private function mouseDownHandler(e:MouseEvent):void{
			arrow.visible = true;
			arrow.setAngle(current.mouseX, current.mouseY);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		private function mouseMoveHandler(e:MouseEvent):void{
			arrow.setAngle(current.mouseX, current.mouseY);
		}
		private function mouseUpHandler(e:MouseEvent):void{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			current.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			current.buttonMode = false;
			arrow.visible = false;
			current.fireScale(arrow.angle, arrow.speed);
			movement.push(current);
			timer.start();
			current.selected(false);
			current = null;
		}
		
		public function execution():Boolean{
			for(var i:uint = 0; i <  movement.length; ){
				var s:Soldier = movement[i];
				if(s.execution()){
					++i;
					checkSoldier(s);//士兵碰撞检测
					checkWalls(s);//墙壁
//					checkBody(s);//刚体碰撞检测
//					checkOther(s)//静动可摧毁物体碰撞检测
//					if(isHit){//临时这样写,之后正式代码的碰撞不需要停止运行对象
//						movement.splice(i, 1);
//					} else {
//						++i;
//					}
				} else {
					movement.splice(i, 1);
				}
			}
			return movement.length > 0;
		}
		private function checkWalls(s:MoveBase):void {
			var vx:Number,  vy:Number;
			if (s.x + s.radius > stage.stageWidth - 50) {
				s.x = stage.stageWidth - s.radius - 50;
				vx = s.vx * s.bounce;
			} else if (s.x - s.radius < 50) {
				s.x = s.radius + 50;
				vx = s.vx * s.bounce;
			} else {
				vx = s.vx;
			}
			if (s.y + s.radius > stage.stageHeight - 50) {
				s.y = stage.stageHeight - s.radius - 50;
				vy = s.vy * s.bounce;
			} else if (s.y - s.radius < 50) {
				s.y = s.radius + 50;
				vy = s.vy * s.bounce;
			} else {
				vy = s.vy
			}
			if(vy != s.vy || vx != s.vx){
				var sv:Number = Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2));
				var sa:Number = Math.atan2(vy, vx);
				s.fire(sa, sv);
			}
		}
		/**
		 * 士兵碰撞检测
		 */
		public function checkSoldier(s:MoveBase):void{
			var check:MoveBase;
			for(var i:uint = 0; i < soldiers.length; ++i){
//			for each(check in soldiers){
				check = soldiers[i];
				if(check == s){
					continue;
				}
				var dx:Number = check.x - s.x;
				var dy:Number = check.y - s.y;
				var dist:Number = Math.sqrt(dx*dx + dy*dy);
				if (dist < s.radius + check.radius) {
					// 计算角度和正余弦值
					var angle:Number = Math.atan2(dy, dx);
					var sin:Number = Math.sin(angle);
					var cos:Number = Math.cos(angle);
					// 旋转 s 的位置
					var pos0:Point = new Point(0, 0);
					// 旋转 check 的速度
					var pos1:Point = rotate(dx, dy, sin, cos, true);
					// 旋转 s 的速度
					var vel0:Point = rotate(s.vx, s.vy,
						sin, cos, true);
					// 旋转 check 的速度
					var vel1:Point = rotate(check.vx, check.vy,
						sin, cos, true);
					// 碰撞的作用力
					var vxTotal:Number = vel0.x - vel1.x;
					vel0.x = ((s.quality - check.quality) * vel0.x +
						2 * check.quality * vel1.x) /
						(s.quality + check.quality);
					vel1.x = vxTotal + vel0.x;
					// 更新位置
					var absV:Number = Math.abs(vel0.x) + Math.abs(vel1.x);
					var overlap:Number = (s.radius + check.radius) - Math.abs(pos0.x - pos1.x);
					pos0.x += vel0.x / absV * overlap;
					pos1.x += vel1.x / absV * overlap;
					//				pos0.x += vel0.x;
					//				pos1.x += vel1.x;
					// 将位置旋转回来
					var pos0F:Object = rotate(pos0.x, pos0.y,
						sin, cos, false);
					var pos1F:Object = rotate(pos1.x, pos1.y,
						sin, cos, false);
					// 将位置调整为屏幕的实际位置
					check.x = s.x + pos1F.x;
					check.y = s.y + pos1F.y;
					s.x = s.x + pos0F.x;
					s.y = s.y + pos0F.y;
					// 将速度旋转回来
					var vel0F:Object = rotate(vel0.x, vel0.y,
						sin, cos, false);
					var vel1F:Object = rotate(vel1.x, vel1.y,
						sin, cos, false);
					
					var sv:Number = Math.sqrt(Math.pow(vel0F.x, 2) + Math.pow(vel0F.y, 2));
					var sa:Number = Math.atan2(vel0F.y, vel0F.x);
					s.fire(sa, sv);
					var cv:Number = Math.sqrt(Math.pow(vel1F.x, 2) + Math.pow(vel1F.y, 2));
					var ca:Number = Math.atan2(vel1F.y, vel1F.x);
					check.fire(ca, cv);
					check.collide = s.collide;
					if(check.life <= 0){
						soldiers.splice(i, 1);
						--i;
					} else {
						movement.push(check);
					}
				}
			}
		}
		private function rotate(x:Number, y:Number,
								sin:Number, cos:Number, reverse:Boolean):Point {
			var result:Point = new Point();
			if (reverse) {
				result.x = x * cos + y * sin;
				result.y = y * cos - x * sin;
			} else {
				result.x = x * cos - y * sin;
				result.y = y * cos + x * sin;
			}
			return result;
		}
		/**
		 * 刚体碰撞检测
		 */
		public function checkBody(s:MoveBase):Boolean{
			return false;
		}
		/**
		 * 静动可摧毁物体碰撞检测
		 */
		public function checkOther(s:MoveBase):Boolean{
			return false;
		}
		private function timerHandler(e:TimerEvent):void{
			if(!execution()){
				timer.stop();
				nextSoldier();
			}
		}
		public function clear():void{
			while(soldiers.length > 0){
				var s:Soldier = soldiers.pop()as Soldier;
				removeChild(s);
			}
			while(movement.length > 0){
				movement.pop();
			}
		}
	}
}